//
//  OMGoodTwoColumnCollectDelegate.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/22.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "OMGoodTwoColumnCollectDelegater.h"
#import "OMGoodCollectCell.h"
#import "UIScrollView+EmptyDataSet.h"
@interface OMGoodTwoColumnCollectDelegater ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *collectView;
@end

@implementation OMGoodTwoColumnCollectDelegater
- (instancetype)initWithCollectionView:(UICollectionView*)collectView
{
    self = [super init];
    _collectView = collectView;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.emptyDataSetSource = self;
    _collectView.emptyDataSetDelegate = self;
    [self configure];
    return self;
}

- (void)configure
{
    [_collectView registerClass:[OMGoodCollectCell class] forCellWithReuseIdentifier:[OMGoodCollectCell cellIdentifier]];
}

- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    [self.collectView reloadData];
}

#pragma mark - datasource && delegate FOR collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _goods.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMGoodCollectCell *cell = [OMGoodCollectCell collectCellWithCollectionView:collectionView
                                                                   atIndexPath:indexPath];
    cell.layer.borderColor = HEXCOLOR(@"#E9E9EA").CGColor;
    cell.layer.cornerRadius = SM_SIZE(5);
    cell.clipsToBounds = true;
    cell.layer.borderWidth = 1.f;
    [cell configureCellWithObj:self.goods[indexPath.item]];
    cell.contentView.backgroundColor = SM_whiteColor;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMGoodDetailViewController", @{@"productId":[self.goods[indexPath.item] id]});
}


#pragma mark - DZNEmptyDataSetSource
//空tableview 的处理(完全得益于DZNEmptyDataSetSource),获得该处理，子类需要遵守相关的协议并设置相关的代理
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];

    text = @"唉呀,没找到相应商品!";
    font = SM_CFont(32);
    textColor = SM_blackColor;
    [attributes setObject:@(0.45) forKey:NSKernAttributeName];
    if (!text) {
        return nil;
    }
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (self.isLoading) {
        return nil;
    }
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}



- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.isLoading) {
        return nil;
    }
    return [UIImage st_imageNamed:@"NoGoodsEmpty@2x" type:@"png"];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    //to override
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return SM_SIZE(100);
}

// empty data function system core
- (void)setLoading:(BOOL)loading
{
    _loading = loading;
    [self.collectView reloadEmptyDataSet];
}
@end
